<template>
  <el-card>
    <el-table v-loading="loading" :data="dataList" default-expand-all row-key="id" border>
      <el-table-column prop="name" label="名称" align="center"></el-table-column>
      <el-table-column prop="code" label="权限职位" align="center"></el-table-column>
      <el-table-column prop="updateTime" label="修改时间" align="center"></el-table-column>
      <el-table-column label="操作" width="350" align="center">
        <template v-slot="{row}">
          <el-button type="warning" icon="Plus" v-if="row.level !== 4" @click="openDialog(row.level === 3? '添加功能': '添加菜单', false, row)" v-role-button="`btn.Permission.add`">
            {{ row.level === 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button type="primary" icon="Edit" v-if="row.level !== 1" @click="openDialog(row.level === 3? '修改功能': '修改菜单', true, row)" v-role-button="`btn.Permission.update`">编辑</el-button>
          <el-popconfirm :title="`你确定要删除${row.name}吗？`" width="260" @confirm="deleteMenu(row.id)" v-if="row.level !== 1" v-role-button="`btn.Permission.remove`">
            <template #reference>
              <el-button type="danger" icon="Delete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" :title="dialogTitle" @closed="closeDialog">
      <el-form label-width="80" style="width: 80%" :model="menuForm" :rules="menuRules" ref="menuFormRef">
        <el-form-item label="菜单名称" prop="name">
          <el-input placeholder="请输入菜单名称" v-model="menuForm.name"></el-input>
        </el-form-item>
        <el-form-item label="权限值" prop="code">
          <el-input placeholder="请输入菜单权限" v-model="menuForm.code"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveMenu">确认</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import {ref, onMounted, reactive, useTemplateRef} from 'vue';
import type {menuAddOrUpdateInter, permissionMenuInter} from "@/api/acl/menu/type.ts";
import {reqAllPermissionMenu, reqDeletePermissionMenu, reqMenuAddOrUpdate} from "@/api/acl/menu";
import type {ResponseInter} from "@/api/commonInterFace.ts";
import {ElMessage, type FormInstance} from "element-plus";

let dataList = ref<permissionMenuInter[]>([]);
let loading = ref<boolean>(false)

onMounted(() => {
  getDataList()
})

const getDataList = async () => {
  loading.value = true
  let res = await reqAllPermissionMenu()
  if (res.code === 200) {
    dataList.value = res.data
  }
  loading.value = false
}

// 弹窗
let dialogVisible = ref<boolean>(false)
let dialogTitle = ref<string>('')
// 菜单表单
let menuForm = reactive<menuAddOrUpdateInter>({
  code: '',
  level: 0,
  name: '',
  pid: 0
})
let menuFormRef = useTemplateRef<FormInstance>('menuFormRef')

let menuRules = {
  name: [
    {required: true, message: '请输入菜单名称', trigger: 'blur'}
  ],
  code: [
    {required: true, message: '请输入菜单权限', trigger: 'blur'}
  ]
}

// 打开弹窗
const openDialog = (title: string, isEdit: boolean, row: permissionMenuInter) => {
  dialogTitle.value = title
  dialogVisible.value = true
  if (!isEdit) {
    menuForm.level = row.level + 1
    menuForm.pid = row.id as number
  } else {
    Object.assign(menuForm, row)
  }
}
// 关闭弹窗
const closeDialog = () => {
  Object.assign(menuForm, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0
  })
}

const saveMenu = async () => {
  await menuFormRef.value?.validate(async valid => {
    if (!valid) return
    let res: ResponseInter = await reqMenuAddOrUpdate(menuForm)
    if (res.code === 200) {
      ElMessage.success(res.message)
      dialogVisible.value = false
      await getDataList()
    } else {
      ElMessage.error(res.message)
    }
  })
}

// 删除菜单
const deleteMenu = async (id: number) => {
  let res: ResponseInter = await reqDeletePermissionMenu(id)
  if (res.code === 200) {
    ElMessage.success(res.message)
    await getDataList()
  } else {
    ElMessage.error(res.message)
  }
}
</script>

<style scoped lang="scss">

</style>
